﻿using AutoMapper;
using Factory.EquipmentManagement.CheckMaintenanceItems.Dto;
using Factory.EquipmentManagement.CheckMaintenanceItems;
using Factory.ProductionManagement.ProcedureSettings.Dto;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;
using Factory.MasterData.Materials.Dto;

namespace Factory.ProductionManagement.ProcedureSettings
{
    /// <summary>
    /// 工序设置服务
    /// </summary>
    public class ProcedureSettingAppService: ApplicationService, IProcedureSettingAppService
    {
        /// <summary>
        /// 工序设置仓储接口
        /// </summary>
        private readonly IRepository<ProcedureSetting,int> _procedureSettingRepository;
        /// <summary>
        /// 映射接口
        /// </summary>
        private readonly IMapper _mapper;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="procedureSettingRepository">工序设置仓储接口</param>
        /// <param name="mapper">映射接口</param>
        public ProcedureSettingAppService(IRepository<ProcedureSetting, int> procedureSettingRepository, IMapper mapper)
        {
            _procedureSettingRepository = procedureSettingRepository;
            _mapper = mapper;
        }
        /// <summary>
        /// 添加重复性验证
        /// </summary>
        /// <param name="code">工序编号</param>
        /// <returns>返回结果</returns>
        public int AddRepeatability(string code)
        {
            long count = _procedureSettingRepository.GetListAsync(m => m.Code == code).Result.Count();
            return count == 0 ? 0 : -1;
        }
        /// <summary>
        /// 添加工序设置
        /// </summary>
        /// <param name="procedureSettingDto">工序设置的数据</param>
        /// <returns>返回结果</returns>
        public async Task<int> Insert(ProcedureSettingDto procedureSettingDto)
        {
            var reslut = AddRepeatability(procedureSettingDto.Code!);
            if (reslut != 0) return -1;
            ProcedureSetting procedureSetting = _mapper.Map<ProcedureSetting>(procedureSettingDto);
            await _procedureSettingRepository.InsertAsync(procedureSetting);
            return 1;
        }
        /// <summary>
        /// 工序设置信息分页显示
        /// </summary>
        /// <param name="pageIndex">当前页</param>
        /// <param name="pageSize">页容量</param>
        /// <param name="inquireCode">工序设置编码</param>
        /// <returns>返回结果</returns>
        public async Task<PageResult> PageList(int pageIndex, int pageSize, string? inquireCode)
        {
            var list = await _procedureSettingRepository.GetListAsync(m => string.IsNullOrEmpty(inquireCode) || m.Code!.Contains(inquireCode));
            PageResult pageResult = new PageResult();
            pageResult.RowCount = list.Count();
            pageResult.PageCount = (int)Math.Ceiling(pageResult.RowCount * 1.0 / pageSize);
            pageResult.Queryable = list.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
            return pageResult;
        }
        /// <summary>
        /// 删除工序设置信息
        /// </summary>
        /// <param name="id">工序设置主键</param>
        /// <returns>返回结果</returns>
        public async Task<int> Deleted(string id)
        {
            var ids = id.Split(',');
            foreach (var item in ids)
            {
                await _procedureSettingRepository.DeleteAsync(m => m.Id == Convert.ToInt32(item));
            }
            return 1;
        }
        /// <summary>
        /// 编辑重复性验证
        /// </summary>
        /// <param name="code">供应商编码</param>
        /// <param name="id">供应商主键</param>
        /// <returns>返回结果</returns>
        public async Task<int> UpdateRepeatability(string code, int id)
        {
            var list = await _procedureSettingRepository.GetListAsync(m => m.Code == code && m.Id != id);
            return list.Count() > 0 ? -1 : 0;
        }
        /// <summary>
        /// 编辑工序设置信息
        /// </summary>
        /// <param name="procedureSettingDto">编辑的供应商信息</param>
        /// <returns>返回结果</returns>
        public async Task<int> Update(ProcedureSettingDto procedureSettingDto)
        {
            ProcedureSetting procedureSetting = _mapper.Map<ProcedureSetting>(procedureSettingDto);
            await _procedureSettingRepository.UpdateAsync(procedureSetting);
            return 1;
        }
    }
}
